package api

import (
	"fmt"
	"template/generation/generation"

	"github.com/spf13/cobra"
)

var (
	engineering string
	port        string
	namespace   string
	directory   string // New variable for directory
	types       string
	giturl      string
	viewname    string
	StartCmd    = &cobra.Command{
		Use:     "create",
		Short:   "配置文件生成器",
		Example: "configctl create -e [proj name] -p [proj port] -n [proj namespace] -t [backend|node] -d [directory] -g [git url] -view [jenkins view name]",
		Run: func(cmd *cobra.Command, args []string) {
			if engineering == "" || namespace == "" || port == "" || types == "" || giturl == "" || viewname == "" {
				cmd.Help()
				return
			}

			// 检查types参数是否有效
			if types != "backend" && types != "node" {
				fmt.Println("types不正确，可选参数为：[backend|node]")
				return // 提前返回，不继续执行后续代码
			}

			ad := generation.AutoCodeService{}
			str, err := ad.CreatePackageTemp(engineering, port, namespace, directory, types, giturl, viewname) // Modify this line as needed
			if err != nil {
				panic(err)
			}
			fmt.Println(str)
		},
	}
)

func init() {
	// 第一参数位为：为go定义的参数变量
	// 第二参数位为：为外部完整参数，如：configcil --engineering test
	// 第三参数位为：为简化参数，如:configcil -e test
	// 第四参数位为：为默认参数变量，如果命令行不写-e参数的话，就取默认值
	// 第五参数位为：为注解，会写道help里面
	StartCmd.PersistentFlags().StringVarP(&engineering, "engineering", "e", "", "[输入要生成的项目名称]")
	StartCmd.PersistentFlags().StringVarP(&port, "port", "p", "", "[输入项目端口号]")
	StartCmd.PersistentFlags().StringVarP(&namespace, "namespace", "n", "", "[K8s命名空间名称]")
	StartCmd.PersistentFlags().StringVarP(&directory, "directory", "d", "", "[指定目录路径]") // Registering the directory flag
	StartCmd.PersistentFlags().StringVarP(&types, "types", "t", "", "[指定项目类型]")
	StartCmd.PersistentFlags().StringVarP(&giturl, "giturl", "g", "", "[指定git地址]") // Registering the giturl flag
	StartCmd.PersistentFlags().StringVarP(&viewname, "viewname", "v", "", "[指定jenkins视图名称]")

}
